#ifndef SCHEDULETHREAD_H
#define SCHEDULETHREAD_H

#include <QThread>
#include <QVector>
#include <QMutex>
#include <QWaitCondition>
#include <QDebug>
#include <QQueue>

class ScheduleObject
{
public:

    ScheduleObject() {
        _func = 0;
        memset(_args, 0, sizeof(_args));
    }

    ScheduleObject(void *func, void *p1, void *p2, void *p3) {
        _func = func;

        _args[0] = p1;
        _args[1] = p2;
        _args[2] = p3;
    }

    ScheduleObject & operator ==(const ScheduleObject & obj) {

        _func = obj._func;
        _args[0] = obj._args[0];
        _args[1] = obj._args[1];
        _args[2] = obj._args[2];
        return *this;
    }

    void *_func;
    void *_args[4];
};


class ScheduleThread : public QThread
{
    Q_OBJECT
public:
    explicit ScheduleThread(QObject *parent = 0);
    ~ScheduleThread();
    
    void run();
    void schedule(void *func, void *p1 = 0, void *p2 = 0, void *p3 = 0);


    void waitForFinished();

private:

    bool finish;
    QMutex mutex, wait_mutex;
    QQueue <ScheduleObject> objects;
    int waits_cnt;
    QWaitCondition wait_cond;

};

#endif // SCHEDULETHREAD_H
